package com.fz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fz.entity.Repair;
import com.fz.mapper.RepairMapper;
import com.fz.service.RepairService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 存储用户报修信息 服务实现类
 * </p>
 *
 * @author hyr
 * @since 2025-06-18
 */
@Service
public class RepairServiceImpl extends ServiceImpl<RepairMapper, Repair> implements RepairService {
    @Resource
    RepairMapper repairMapper;
    @Override
    public boolean check(Long id,String field,String value)
    {
        QueryWrapper<Repair> wrapper=new QueryWrapper<>();
        wrapper.eq(field,value);
        System.out.println(id);
        if(null!=id)
        {
            wrapper.ne("id",id);
        }
        return this.count(wrapper)==0;
    }
    @Override
    public boolean pass(Long id,Integer status,Integer statu){
        return this.updateById(new Repair(id,status,statu));
    }
    @Override
    public boolean reject(Long id,Integer status,Integer statu){
        return this.updateById(new Repair(id,status,statu));
    }

    @Override
    public boolean del(Long id, Integer status) {
        return this.updateById(new Repair(id,status));
    }

    @Override
    public Page<Repair> getList(Long userId, Integer pageNo, Integer pageSize) {
        QueryWrapper<Repair> wrapper = new QueryWrapper<>();
        Page<Repair>repairPage;
        wrapper.eq("user_id", userId)
                .ne("status",-1)
                .orderByDesc("repair_time");
        repairPage = this.page(new Page<>(pageNo, pageSize), wrapper);
        return this.page(new Page<>(pageNo, pageSize),wrapper);
    }
    @Override
    public Page<Repair> search(String input, Integer pageNo, Integer pageSize){
        QueryWrapper<Repair> wrapper=new QueryWrapper<>();
        Page<Repair>repairPage;
        wrapper.like("type",input)
                .ne("status",-1)
                .orderByDesc("repair_time");
        repairPage = this.page(new Page<>(pageNo, pageSize), wrapper);
        return this.page(new Page<>(pageNo, pageSize),wrapper);
    }
    @Override
    public Page<Repair> showList(Integer pageNo, Integer pageSize) {
        QueryWrapper<Repair> wrapper = new QueryWrapper<>();
        Page<Repair>repairPage;
        wrapper.ne("status",-1)
                .orderByDesc("repair_time");
        repairPage = this.page(new Page<>(pageNo, pageSize), wrapper);
        return this.page(new Page<>(pageNo, pageSize),wrapper);
    }
    @Override
    public Boolean update(Long id, String type, String content, LocalDateTime repairTime){
        Repair repair = this.getById(id);
        repair.setType(type);
        repair.setContent(content);
        repair.setRepairTime(repairTime);
        return this.updateById(repair);
    }
    @Override
    public Boolean add(Integer userId,String type,String content,LocalDateTime repairTime,Integer status,String username){

        return this.save(new Repair(Long.valueOf(userId),type,content,repairTime,status,username));

    }
    @Override
    public String getByRepairId(Long RepairId,Long userId){//根据id查找
        return repairMapper.getByRepairId(RepairId,userId);
    }
    @Override
    public boolean update(Long id, String username){
        return this.updateById(new Repair(id,username));
    }
    @Override
    public List<Repair> updateview(List<Repair> repairList) {
        List<Repair> updatedRepairList = new ArrayList<>();

        for (Repair repair : repairList) {//循环
            Long id=repair.getId();
            Long userId= Long.valueOf(repair.getUserId());
            if(userId!=null){
                String name = this.getByRepairId(id,userId);//
                System.out.println(name);
                if (this.update(id,name)) {//如果更新成功
                    System.out.println("id="+id);
                    updatedRepairList.add(repair);//把更新后的放到新的表里
                    System.out.println("成功，通知ID: " + id);
                } else {
                    System.out.println("错误，通知ID: " + id);
                }
            }else{
                continue;
            }
        }

        return updatedRepairList;
    }

}
